package com.basic.java.system;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * 类
 * 创建时间：2021/12/27 21:36
 *
 * @author gejin
 */
public class UserCredentials {
    /**
     * 读取姓名
     *
     * @param length 长度
     * @return
     * @throws IOException
     */
    public String readUsername(int length) throws IOException {
        byte[] name = new byte[length];

        System.in.read(name, 0, length);
        return new String(name);
    }

    /**
     * BufferedReader 读取
     *
     * @return
     */
    public String readUsername() throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        return reader.readLine();
    }

    public String readUsernameWithPrompt() {
        Console console = System.console();
        //如果console 是null 证明没有获取到console
        return console == null ? null : console.readLine("%s", "Enter your name: ");
    }

}
